﻿Partial Class App_dm
    Inherits MyUI.PageBaseApp

    Protected SortGridLocations As UI.SortControl
    Public CurrAccount As mDB.Account

    Dim FolderParentId As Integer
    <Services.WebMethod()> _
    Public Shared Function AutoCompleteHashTags(ByVal k As String) As String()

        Dim HashTags As Dictionary(Of String, Integer) = HttpContext.Current.Session("HashTags")

        If HashTags Is Nothing Or k.Trim = "" Then
            Return {""}
        End If

        Dim a = HashTags.Where(Function(o) o.Key.Substring(1).StartsWith(k)).OrderByDescending(Function(o) o.Value).Select(Function(o) o.Key).Take(100).ToArray

        Return a

    End Function

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Items("RootFolderID") = -1
        FolderParentId = Utils.Conv.ToInt(Me.HidSelectFolderId.Value) ' Me.HidSelectFolderId.Value

        SortGridLocations = New UI.SortControl(Me.GridElaborations.ID, Me.ViewState, "Query", SortDirection.Ascending)

        ScriptManager.GetCurrent(Page).EnablePageMethods = True

        CurrAccount = CurrUser.CurrAccount

        If Not Me.IsPostBack Then
            SetDataPage()
        Else

        End If



    End Sub
    Public Sub SetDataPage()




    End Sub
    '#Region "ACCOUNT"

    '    Protected Sub AccountSelect_SelectedChanged() Handles AccountSelect.SelectedChanged
    '        SetDataPage()
    '    End Sub

    '#End Region

#Region "QUERY"

    Public Sub SetListQuery(AddRow As Boolean)

        Me.SetListQuery(AddRow, New mDB.Query)

    End Sub
    Public Sub SetListQuery(AddRow As Boolean, QueryDef As mDB.Query)
        Dim g = Me.GridElaborations

        Dim dt = CurrAccount.Tab_Private_Post.REL.Private_Post.ToAccount.ToDataTable

        'If AddRow Then
        '    'Utils.DT.AddDataRowAt(qs, UI.Grid.GetIndexDataFromRow(g, 0))
        '    dt.Insert(UI.Grid.GetIndexData(g, 0), QueryDef)
        '    g.EditIndex = 0
        'End If


        '-------------------------------------------------------
        ' LAYOUT
        '-------------------------------------------------------

        g.PageSize = 30
        UI.Grid.Bind(g, dt, {"id"})


        For Each gr As GridViewRow In g.Rows
            Dim query = dt(UI.GridRow.GetIndexData(gr))
            Dim LblStatus = UI.GridRow.GetLabel(gr, "LblStatus")
            Dim LblPlace = UI.GridRow.GetLabel(gr, "LblPlace")



        Next

    End Sub
    Protected Sub BtnReload_UpPElaborations_Click(sender As Object, e As System.EventArgs) Handles BtnReload_UpPElaborations.Click

        SetListQuery(False)


    End Sub
    Protected Sub GridElaborations_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridElaborations.PageIndexChanging

        GridElaborations.PageIndex = e.NewPageIndex

        SetListQuery(False)
    End Sub
    Protected Sub GridElaborations_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridElaborations.RowCommand
        Dim g As GridView = sender

        Dim id = UI.Grid.GetKey(g, e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim gr = UI.GridRow.GetRow(e.CommandSource)

        Dim query = DB.TAB.Query.GetByPK(id)


        Select Case e.CommandName
            Case "EDIT_ROW"
                g.EditIndex = gr.RowIndex
                SetListQuery(False)

            Case "CANCEL_ROW_EDIT"

                g.EditIndex = -1
                SetListQuery(False)

            Case "UPDATE_ROW"

                If query Is Nothing Then
                    query = (New mDB.Query With {.Folder_Id = Me.FolderParentId}).SetDefault
                    DB.TAB.Query.InsertOnSubmit(query)
                End If

                query.Query = UI.GridRow.GetTextBox(gr, "TxtQuery").Text
                query.Place = UI.GridRow.GetTextBox(gr, "TxtPlace").Text
                query.Lang_Code = UI.GridRow.GetTextBox(gr, "TxtLangCode").Text
                query.Description = UI.GridRow.GetTextBox(gr, "TxtDescription").Text
                query.Network_Id = MyApp.Network.AccType.TWITTER_ACCOUNT

                DB.SubmitChanges()

                g.EditIndex = -1
                SetListQuery(False)

            Case "DELETE_ROW"
                DB.TAB.Query.DeleteOnSubmit(query)
                DB.SubmitChanges()
                SetListQuery(False)

            Case "LOAD"



            Case "CLEAR"


            Case "INDEX"
                MyApp.Task.Elaborate.CreateIndexQueryPosts(query.Id)

            Case "DOWNLOAD"
                query.ReIndex()
                DB.SubmitChanges()
                SetListQuery(False)

            Case "REAL_TIME_ENABLE"
                'query.Real_Time = 1
                DB.SubmitChanges()
                SetListQuery(False)


            Case "REAL_TIME_DISABLE"
                'query.Real_Time = 0
                DB.SubmitChanges()
                SetListQuery(False)


        End Select




    End Sub
    Protected Sub BtnAddQuery_Click(sender As Object, e As System.EventArgs) Handles BtnAddQuery.Click

        SetListQuery(True)

    End Sub
    Protected Sub GridElaborations_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridElaborations.Sorting

        Me.SortGridLocations.Sort(e.SortExpression)

        Me.SetListQuery(False)
    End Sub

#End Region

#Region "ANALISYS"


#End Region

#Region "POST"


#End Region


    'Public Sub SetDataSearch(q As mDB.Query)

    '    Dim AD = mDB.Archive_Simil_Text.GetArchive(mDB.Archive_Simil_Text.ArchiveType.POST_QUERY, q.Id).GetSimilTextArchiveData

    '    dic = AD.Dictionary


    '    Dim f As New SimilText.FindSimilarText(AD)

    '    f.Text = ""
    '    f.RankMin = 0
    '    f.Find()
    '    f.Close()


    '    Me.LblExec.Text = String.Format("Analisi effettuata in {0} sec.", f.Result.ExecTime)

    '    '----------------------------------------------------------
    '    'trova le parole + utilizzate
    '    'Dim wids As New Dictionary(Of UInteger, Integer)
    '    'For Each r In f.Result.Matches
    '    '    For ct = 0 To r.Seq.Length - 1

    '    '        If wids.ContainsKey(r.Seq(ct)) Then
    '    '            wids(r.Seq(ct)) += 1
    '    '        Else
    '    '            wids.Add(r.Seq(ct), 1)
    '    '        End If

    '    '    Next

    '    'Next
    '    Dim wids = f.Result.WordCluster1


    '    TotHashtag = wids.Sum(Function(o) o.Value)

    '    seq = New SimilText.TextTools(q.Query, AD).TextToNumber.ToDictionary(Of UInteger)(Function(o) o)

    '    Dim hash_ids = wids.Where(Function(o) AD.Dictionary.WordsList(o.Key).IsHashtag).OrderByDescending(Function(o) o.Value).Take(100).ToList
    '    Dim word_ids = wids.Where(Function(o) Not AD.Dictionary.WordsList(o.Key).IsHashtag).OrderByDescending(Function(o) o.Value).Take(100).ToList
    '    'Dim lwids = wids.Where(Function(o) f.TI.Dictionary.WordsList(o.Key).IsHashtag).OrderByDescending(Function(o) o.Value).Take(100).ToList
    '    TotCloudHashtag = hash_ids.Sum(Function(o) o.Value)
    '    TotCloudword = word_ids.Sum(Function(o) o.Value)

    '    Me.RptHasttag.DataSource = hash_ids
    '    Me.RptHasttag.DataBind()

    '    Me.RptWord.DataSource = word_ids
    '    Me.RptWord.DataBind()



    '    '----------------------------------------------------------
    '    ' trova i cluster
    '    Clusters = New Dictionary(Of String, Integer)
    '    'For Each r In f.Result.Matches
    '    '    Clustered(r.Seq, 2)
    '    'Next

    '    Dim clus_ord2 = Clusters.OrderByDescending(Function(o) o.Value).Take(20).ToList

    '    Me.RptCluster2.DataSource = clus_ord2
    '    Me.RptCluster2.DataBind()


    '    Clusters = New Dictionary(Of String, Integer)
    '    'For Each r In f.Result.Matches
    '    '    Clustered(r.Seq, 3)
    '    'Next

    '    Dim clus_ord3 = Clusters.OrderByDescending(Function(o) o.Value).Take(20).ToList

    '    Me.RptCluster3.DataSource = clus_ord3
    '    Me.RptCluster3.DataBind()

    '    '----------------------------------------------------------
    '    ' images


    '    'Dim DtImage = q.Tab_QueryPost.
    '    '    Where("image_url", "<>", "").
    '    '    OrderBy("share desc", "tot_foll desc").
    '    '    GroupBy("image_url").
    '    '    SetSelect("image_url", "sum(account_follower_count) as tot_foll", "if(share_count > 0 ,1 ,0) as share").
    '    '    Limit(24).
    '    'ToDataTable()

    '    Dim DtImage = q.Tab_QueryPost.
    '        Where("image_url", "<>", "").
    '        OrderBy("share_count desc", "account_follower_count desc").
    '        GroupBy("image_url").
    '        SetSelect("image_url").
    '        Limit(24).
    '    ToDataTable()


    '    Me.RptImages.DataSource = DtImage
    '    Me.RptImages.DataBind()


    'End Sub








End Class
